﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace ToggleLanguage.Helper
{
    public class HttpModule : IHttpModule
    {
        #region IHttpModule Members

        /// <summary>
        /// module name
        /// </summary>
        public String ModuleName
        {
            get { return Utilities.HttpModuleName; }
        }

        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {

            context.PreRequestHandlerExecute +=
         (new EventHandler(this.Application_BeginRequest));

        }
        /// <summary>
        /// executed for each request http
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        private void Application_BeginRequest(Object source,
       EventArgs e)
        {

            HttpApplication httpApp = (HttpApplication)source;
            HttpContext context = httpApp.Context;
            string httpUrl = context.Request.Url.ToString();
            CultureInfo culture = null;

            string filePath = context.Request.FilePath;
            string fileExtension =
                VirtualPathUtility.GetExtension(filePath);

            string languageCookie = "";//contain the language defined in the client
            //search the cookies
            if (context.Request.Cookies.AllKeys.Any(s => s == Utilities.CookieLanguage))
                languageCookie = context.Request.Cookies[Utilities.CookieLanguage].Value;

            #region Set language
            if (fileExtension.Equals(".aspx"))
            {

                //Accept-Language used in Sharepoint 2013
                var lang = context.Request.Headers["Accept-Language"];

                try
                {


                    if (!string.IsNullOrEmpty(languageCookie))
                    {
                        if (!lang.Contains(languageCookie))
                        {
                            context.Request.Headers["Accept-Language"] = languageCookie + "," 
                                + context.Request.Headers["Accept-Language"];

                        }
                        context.Request.UserLanguages[0] = languageCookie;
                        culture = new CultureInfo(languageCookie);
                        //set the culture
                        Thread.CurrentThread.CurrentCulture = culture;
                        Thread.CurrentThread.CurrentUICulture = culture;
                    }
                }
                catch (Exception ex)
                {
                    //client do not have cookie, or tha navigator disactivate the cookies
                    
                }
            }
            #endregion


     
        }

        #endregion

    }
}
